package com.web.quartz.service.impl;

import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.listeners.SchedulerListenerSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.web.common.basic.quartz.entity.QuartzJobOperateLog;

@Component
public class QuartzSchedulerListener extends SchedulerListenerSupport {
	
	@Autowired
	private QuartzOperateLogServiceImpl quartzOperateLogServiceImpl;

	@Override
	public void jobAdded(JobDetail jobDetail) {
		JobKey jobKey = jobDetail.getKey();
		QuartzJobOperateLog log = new QuartzJobOperateLog();
		log.setJobName(jobKey.getName());
		log.setJobGroup(jobKey.getGroup());
		log.setDescription(jobDetail.getDescription());
		log.setJobClassName(jobDetail.getClass().getName());
		log.setOperateResult("新增定时任务");
		quartzOperateLogServiceImpl.saveOperateLog(log);
	}
	
	@Override
	public void jobPaused(JobKey jobKey) {
		saveLog(jobKey, "暂停定时任务");
	}
	
	@Override
	public void jobResumed(JobKey jobKey) {
		saveLog(jobKey, "恢复定时任务");
	}
	
	@Override
	public void jobDeleted(JobKey jobKey) {
		saveLog(jobKey, "删除定时任务");
	}
	
	/**
	 * 保存记录操作记录信息
	 * @param jobKey 
	 * @param operateMessage
	 *
	 * @author zhouhui
	 * @since 2023.11.18
	 */
	private void saveLog(JobKey jobKey, String operateMessage) {
		QuartzJobOperateLog log = new QuartzJobOperateLog();
		log.setJobName(jobKey.getName());
		log.setJobGroup(jobKey.getGroup());
		log.setOperateResult(operateMessage);
		quartzOperateLogServiceImpl.saveOperateLog(log);
	}
}
